from struct import *
import sys

# This script is used for reading/writing different kinds of index for aux use.
print "read/write binary file"

def simpleLook():
    print "simpleLook"
    scoreLookFor = 1.11517
    inputFileHandler = open("/data1/team/weijiang/compatibleIndexesWithIRTK/gov2/wei_pruning_development_2012-07-04-15-33-41.ext","rb")
    while True:
        s1 = inputFileHandler.read(4)
        (v1,) = unpack( "1f", s1)
        #print "v1:",v1
        if v1 == scoreLookFor:
            print "Find"

def read():
    print "read"
    block_want_to_show = 97
    chunk_want_to_show_within_block = 373
    NUM_BLOCK = 181
    scoreLookFor = 1.11517
    #scoreLookFor = 0.842548
    #inputFileHandler = open("/home/weijiang/workspace/polyIRToolkit/polyIRIndexer/wei_test_prune_try_3.ext","rb")
    inputFileHandler = open("/data1/team/weijiang/compatibleIndexesWithIRTK/gov2/wei_pruning_development_2012-07-04-15-33-41.ext","rb")
    
    #inputFileHandler = open("/data/rkhmel01/ir_toolkit/index_layered_8_exp_non_overlapping.ext","rb")
    blockCounter = 0
    postingCounter = 0
    
    for j in range(0,NUM_BLOCK):
        s1 = inputFileHandler.read(4 + 4)
        (v1,v2) = unpack( "1i1f", s1)
        
        print "block_num:",j
        print "block_num_chunks:",v1
        print "Block Max:",v2
        print
        
        for i in range(0,int(v1)):
            s1 = inputFileHandler.read(4)
            (v3,) = unpack( "1f", s1)
            if j == block_want_to_show:
                print "--->chunkCounter:",i,"Chunk Max",":",v3
            if v2 < v3:
                print "P1"
                sys.exit()
            
            for k in range(0,128):
                s1 = inputFileHandler.read(4)
                (v4,) = unpack( "1f", s1)
                postingCounter += 1
                
                if j == block_want_to_show and i == chunk_want_to_show_within_block:
                    print k,v4
                
                '''
                if v4 == scoreLookFor:
                    print "Find match"
                    print "curr_block_num:",j
                    print "curr_chunk_num:",i
                    print "curr_document_offset:",k
                ''' 
                    
                
                if postingCounter > 8400333:
                    break
                if v2 < v4:
                    print "P2"
                    sys.exit()
                
            
        
            
    inputFileHandler.close()
    print "postingCounter:",postingCounter
    
def write():
    print "write"
    outputFileHandler = open("/home/weijiang/workspace/polyIRToolkit/polyIRIndexer/testBi.ext","wb")
    outputFileHandler.write(pack("66f", 1.40129846432e-45, 15.9015455246, 15.9015455246,4.82374,5.14076,8.56119,3.10649,1.57827,5.12863,9.19079,6.88509,13.779,5.63311,5.11737,13.66,11.8439,16.0316,9.33557,15.2096,15.9656,5.40692,9.77615,15.2294,15.8159,15.4182,8.6593,11.7666,16.0253,0.511922,0.61234,8.2088,2.17585,6.71241,2.91754,5.81428,12.1112,11.6405,2.90003,7.83524,7.62165,2.90662,2.39162,15.1743,10.1396,11.7676,3.89207,11.2871,15.3711,4.10446,9.10909,0.825253,11.6249,1.2435,4.63435,1.9132,1.47068,15.1274,15.4972,11.2854,13.2075,1.58034,4.35082,3.31788,1.63549,0.755889,2.72143))
    outputFileHandler.close()

#simpleLook()
read()
#write()
print "End"

